package functii.elementare;

import operatori.Cat;
import operatori.Produs;
import functii.Functie;

public class Radical extends Functie{
    public static double calcul(double aux1){
        return Math.sqrt(aux1);
    }

    public double calcul(){
        if(fStang == null)
            System.exit(1);
        return Cat.Calcul(1, Produs.Calcul(2.0, fStang.calcul()));
    }

    public String derivare(){
        if(fStang == null)
            System.exit(1);
        return Produs.concatTermens(Cat.concatTermens("1.0", "2.0"), Cat.concatTermens("1.0", Radical.concatTermens("x")));
    }

    public double calculDerivata(){
        if(fStang == null)
            System.exit(1);
        return Produs.Calcul(Cat.Calcul(1.0, 2.0), Cat.Calcul(1.0, Radical.calcul(x)));
    }

    public String toString(){
        if(fStang == null)
            System.exit(1);
        return concatTermeni(fStang.toString());
    }

    public String concatTermeni(String aux1){
        String aux;
        int nrP;

        nrP = aux1.charAt(0) == '(' ? 0 : 2;

        aux = "sqrt";
        if(nrP != 0) aux += "(";
        aux += aux1;
        if(nrP != 0) aux += ")";

        return aux;
    }
    public static String concatTermens(String aux1){
        String aux;
        int nrP;

        nrP = aux1.charAt(0) == '(' ? 0 : 2;

        aux = "sqrt";
        if(nrP != 0) aux += "(";
        aux += aux1;
        if(nrP != 0) aux += ")";

        return aux;
    }
}
